<script setup>
import { ref, onMounted, onUnmounted, nextTick, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import Room from './Room.vue'
import Order from './Order.vue'
import request from '../../api/request'
import { useStore } from 'vuex'

import { Bell, Refresh } from '@element-plus/icons-vue'
import { ElBadge} from 'element-plus'

const router = useRouter()
const activeComponent = ref('room')
const store = useStore()
const componentKey = ref(0)

const switchComponent = async (component) => {
  activeComponent.value = component
  store.commit('setHasPendingRefunds', false)
}

const handleRefunds = () => {
  if (activeComponent.value === 'order') {
    componentKey.value++ 
  }
  switchComponent('order')
  store.commit('setHasPendingRefunds', true)
}

const logout = () => {
  if (confirm('确定要退出登录吗？')) {
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    router.replace({ path: '/login' })
    alert('已退出员工端登录')
  }
}

const hasPendingRefunds = ref(false)

const checkPendingRefunds = async () => {
  try {
    const response = await request({
      url:'/employee/booking-order',
      method: 'post',
      data: {
        status: 5,
        page: 1,
        pageSize: 1
      }
    })
    hasPendingRefunds.value = response.data.records.length > 0
  } catch (error) {
    console.error('Error fetching pending refunds:', error)
  }
}

let checkTimer

onMounted(() => {
  checkPendingRefunds()
  // 每30秒检查一次待处理退款订单
  checkTimer = setInterval(checkPendingRefunds, 30000)
})

onUnmounted(() => {
  if (checkTimer) {
    clearInterval(checkTimer)
  }
})

</script>

<template>
  <div class="home-container">
    <header class="header">
      <div class="logo-title-container">
        <div class="logo-container">
          <img src="../../assets/pic/Logo.png" alt="Logo" class="logo-image">
        </div>
        <div class="header-title">MeetKey 议启</div>
      </div>
      <nav class="nav-container">
        <div class="nav-menu">
          <div 
            class="nav-item" 
            :class="{ active: activeComponent === 'room' }"
            @click="switchComponent('room')"
          >
            <i class="nav-icon room-icon"></i>
            <span>会议室列表</span>
          </div>
          <div 
            class="nav-item" 
            :class="{ active: activeComponent === 'order' }"
            @click="switchComponent('order')"
          >
            <i class="nav-icon order-icon"></i>
            <span>订单列表</span>
          </div>
          <div class="nav-slider" :style="{ left: activeComponent === 'room' ? '3%' : '51%' }"></div>
        </div>
      </nav>
      <div class="logout-container">
        <el-popover
          placement="bottom"
          trigger="hover"
          width="200"
        >
          <div >
            有待处理的申请退款订单，
            <span 
              style="color: #409EFF; cursor: pointer; text-decoration: underline;"
              @click="handleRefunds()"
            >
              点击前往
            </span>
          </div>
          <template #reference>
            <el-badge :is-dot="hasPendingRefunds" class="notification-badge">
              <el-icon><Bell /></el-icon>
            </el-badge>
          </template>
        </el-popover>
        <button class="logout-btn" @click="logout">
          <i class="logout-icon"></i>
        </button>
      </div>
    </header>
    
    <main class="main-content">
      <component :is="activeComponent === 'room' ? Room : Order" :key="componentKey"/>
    </main>
  </div>
</template>

<style scoped>
@font-face {
  font-family: 'HanYiYaKuHei';
  src: url('../../assets/fonts/汉仪雅酷黑-85.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
}

.home-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fdecfa;
}

.logo-title-container {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-container {
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 40px;
  padding: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.logo-image {
  width: 36px;
  height: 36px;
  object-fit: contain;
}

.header {
  /* width: 80%; */
  padding: 0 9vh;
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  align-items: center;
  height: 100px;
  position: relative;
  z-index: 10;
}

.nav-container {
  display: flex;
  justify-content: center;
  width: 100%;
}

.header-title {
  color: #2b2b2b;
  font-size: 20px;
  font-family: 'HanYiYaKuHei', sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
}

.nav-container {
  display: flex;
  justify-content: center;
}

.nav-menu {
  display: flex;
  position: relative;
  background-color: #b38bab;
  border-radius: 16px;
  padding: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  width: 300px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.5);
}

.nav-item {
  flex: 1;
  color: #fff;
  padding: 6px 16px;
  cursor: pointer;
  position: relative;
  z-index: 2;
  transition: all 0.3s ease;
  text-align: center;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 500;
  border-radius: 12px;
}

.nav-item:hover {
  color: #f3deef;
}

.nav-item.active {
  color: #9c7293;
  font-weight: 600;
}

.nav-icon {
  display: inline-block;
  width: 18px;
  height: 18px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  filter: brightness(0) invert(1);
  transition: filter 0.3s ease;
}

.nav-item:hover .nav-icon {
  filter: brightness(0) invert(0.95);
}

.nav-item.active .nav-icon {
  filter: invert(55%) sepia(11%) saturate(1035%) hue-rotate(277deg) brightness(84%) contrast(89%);
}

.room-icon {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M19 19H5V5h14v14zm0-16H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-7 9h3v3h-3v-3zm0-5h3v3h-3V7zm-5 5h3v3H7v-3zm0-5h3v3H7V7z"/></svg>');
}

.order-icon {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M19 3h-4.18C14.4 1.84 13.3 1 12 1c-1.3 0-2.4.84-2.82 2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-7 0c.55 0 1 .45 1 1s-.45 1-1 1-1-.45-1-1 .45-1 1-1zm2 14H7v-2h7v2zm3-4H7v-2h10v2zm0-4H7V7h10v2z"/></svg>');
}

.nav-slider {
  position: absolute;
  background-color: rgba(255, 255, 255, 0.9);
  height: calc(100% - 16px);
  width: 46%;
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  top: 8px;
  z-index: 1;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.logout-container {
  width: 200px;
  display: flex;
  align-items: center;
  gap: 16px;
  justify-content: flex-end;
}

.refund-alert {
  position: relative;
  cursor: pointer;
  padding: 8px;
}

.bell-icon {
  display: inline-block;
  width: 20px;
  height: 20px;
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M12 22c1.1 0 2-.9 2-2h-4c0 1.1.9 2 2 2zm6-6v-5c0-3.07-1.63-5.64-4.5-6.32V4c0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5v.68C7.64 5.36 6 7.92 6 11v5l-2 2v1h16v-1l-2-2zm-2 1H8v-6c0-2.48 1.51-4.5 4-4.5s4 2.02 4 4.5v6z"/></svg>');
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  opacity: 0.7;
  transition: opacity 0.3s ease;
}

.refund-alert:hover .bell-icon {
  opacity: 1;
}

.red-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 8px;
  height: 8px;
  background-color: #ff4949;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(0.95);
    box-shadow: 0 0 0 0 rgba(255, 73, 73, 0.7);
  }
  70% {
    transform: scale(1);
    box-shadow: 0 0 0 6px rgba(255, 73, 73, 0);
  }
  100% {
    transform: scale(0.95);
    box-shadow: 0 0 0 0 rgba(255, 73, 73, 0);
  }
}

.logout-btn {
  padding: 0%;
  background-color: rgba(255, 255, 255, 0.8);
  border: none;
  width: 48px;
  height: 48px;
  border-radius: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(179, 139, 171, 0.2);  /* 添加淡紫色边框 */
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.95);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(179, 139, 171, 0.25);  /* 更改阴影颜色为淡紫色 */
  border-color: rgba(179, 139, 171, 0.4);  /* 高亮时边框颜色加深 */
}

.logout-icon {
  display: inline-block;
  width: 20px;  /* 调整图标大小 */
  height: 20px;  /* 调整图标大小 */
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23b38bab"><path d="M17 7l-1.41 1.41L18.17 11H8v2h10.17l-2.58 2.58L17 17l5-5zM4 5h8V3H4c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h8v-2H4V5z"/></svg>');  /* 更改图标颜色为淡紫色 */
  opacity: 0.8;  /* 添加透明度 */
  transition: opacity 0.3s;  /* 添加透明度过渡效果 */
}

.logout-btn:hover .logout-icon {
  opacity: 1;  /* 悬停时图标完全不透明 */
}

.main-content {
  flex: 1;
  /* padding: 20px; */
  background-color: transparent;
  overflow-y: auto;
}
</style>
